package org.jeecg.modules.oms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.oms.entity.SemiProductSalesOrderProgress;
import org.jeecg.modules.oms.service.ISemiProductSalesOrderProgressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.apache.shiro.SecurityUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 半成品销售订单进度Controller
 */
@Api(tags = "半成品销售订单进度管理")
@RestController
@RequestMapping("/oms/semiProductSalesOrderProgress")
@Slf4j
public class SemiProductSalesOrderProgressController extends JeecgController<SemiProductSalesOrderProgress, ISemiProductSalesOrderProgressService> {

    @Autowired
    private ISemiProductSalesOrderProgressService progressService;

    /**
     * 分页列表查询
     */
    @AutoLog(value = "半成品销售订单进度-分页列表查询")
    @ApiOperation(value = "半成品销售订单进度-分页列表查询", notes = "半成品销售订单进度-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<SemiProductSalesOrderProgress>> queryPageList(SemiProductSalesOrderProgress progress,
                                                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                                     HttpServletRequest req) {
        QueryWrapper<SemiProductSalesOrderProgress> queryWrapper = QueryGenerator.initQueryWrapper(progress, req.getParameterMap());
        queryWrapper.orderByDesc("change_time");
        
        Page<SemiProductSalesOrderProgress> page = new Page<SemiProductSalesOrderProgress>(pageNo, pageSize);
        IPage<SemiProductSalesOrderProgress> pageList = progressService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 根据订单ID查询进度记录
     */
    @AutoLog(value = "半成品销售订单进度-根据订单ID查询")
    @ApiOperation(value = "半成品销售订单进度-根据订单ID查询", notes = "半成品销售订单进度-根据订单ID查询")
    @GetMapping(value = "/getByOrderId")
    public Result<List<SemiProductSalesOrderProgress>> getByOrderId(@RequestParam(name = "orderId", required = true) String orderId) {
        try {
            List<SemiProductSalesOrderProgress> progressList = progressService.getByOrderId(orderId);
            return Result.OK(progressList);
        } catch (Exception e) {
            log.error("根据订单ID查询进度记录失败", e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 添加进度记录
     */
    @AutoLog(value = "半成品销售订单进度-添加")
    @ApiOperation(value = "半成品销售订单进度-添加", notes = "半成品销售订单进度-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody SemiProductSalesOrderProgress progress) {
        try {
            boolean result = progressService.addProgress(progress);
            if (result) {
                return Result.OK("添加成功！");
            } else {
                return Result.error("添加失败！");
            }
        } catch (Exception e) {
            log.error("添加进度记录失败", e);
            return Result.error("添加失败：" + e.getMessage());
        }
    }

    /**
     * 编辑进度记录
     */
    @AutoLog(value = "半成品销售订单进度-编辑")
    @ApiOperation(value = "半成品销售订单进度-编辑", notes = "半成品销售订单进度-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody SemiProductSalesOrderProgress progress) {
        try {
            boolean result = progressService.updateProgress(progress);
            if (result) {
                return Result.OK("编辑成功!");
            } else {
                return Result.error("编辑失败!");
            }
        } catch (Exception e) {
            log.error("编辑进度记录失败", e);
            return Result.error("编辑失败：" + e.getMessage());
        }
    }

    /**
     * 通过id删除
     */
    @AutoLog(value = "半成品销售订单进度-通过id删除")
    @ApiOperation(value = "半成品销售订单进度-通过id删除", notes = "半成品销售订单进度-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        try {
            boolean result = progressService.deleteProgress(id);
            if (result) {
                return Result.OK("删除成功!");
            } else {
                return Result.error("删除失败!");
            }
        } catch (Exception e) {
            log.error("删除进度记录失败", e);
            return Result.error("删除失败：" + e.getMessage());
        }
    }

    /**
     * 批量删除
     */
    @AutoLog(value = "半成品销售订单进度-批量删除")
    @ApiOperation(value = "半成品销售订单进度-批量删除", notes = "半成品销售订单进度-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        try {
            String[] idArray = ids.split(",");
            for (String id : idArray) {
                if (oConvertUtils.isNotEmpty(id)) {
                    progressService.deleteProgress(id);
                }
            }
            return Result.OK("批量删除成功!");
        } catch (Exception e) {
            log.error("批量删除进度记录失败", e);
            return Result.error("批量删除失败：" + e.getMessage());
        }
    }

    /**
     * 通过id查询
     */
    @AutoLog(value = "半成品销售订单进度-通过id查询")
    @ApiOperation(value = "半成品销售订单进度-通过id查询", notes = "半成品销售订单进度-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<SemiProductSalesOrderProgress> queryById(@RequestParam(name = "id", required = true) String id) {
        SemiProductSalesOrderProgress progress = progressService.getById(id);
        if (progress == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(progress);
    }

    /**
     * 记录状态变更
     */
    @AutoLog(value = "半成品销售订单进度-记录状态变更")
    @ApiOperation(value = "半成品销售订单进度-记录状态变更", notes = "半成品销售订单进度-记录状态变更")
    @PostMapping(value = "/recordStatusChange")
    public Result<String> recordStatusChange(@RequestParam(name = "orderId", required = true) String orderId,
                                            @RequestParam(name = "oldStatus", required = true) Integer oldStatus,
                                            @RequestParam(name = "newStatus", required = true) Integer newStatus,
                                            @RequestParam(name = "operationType", required = true) String operationType,
                                            @RequestParam(name = "operationContent", required = false) String operationContent,
                                            @RequestParam(name = "changeReason", required = false) String changeReason,
                                            @RequestParam(name = "remark", required = false) String remark) {
        try {
            LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            String operatorId = loginUser != null ? loginUser.getId() : null;
            String operatorName = loginUser != null ? loginUser.getRealname() : null;
            
            boolean result = progressService.recordStatusChange(orderId, oldStatus, newStatus, 
                operatorId, operatorName, operationType, operationContent, changeReason, remark);
            if (result) {
                return Result.OK("状态变更记录成功!");
            } else {
                return Result.error("状态变更记录失败!");
            }
        } catch (Exception e) {
            log.error("记录状态变更失败", e);
            return Result.error("记录失败：" + e.getMessage());
        }
    }

    /**
     * 根据状态查询进度记录
     */
    @AutoLog(value = "半成品销售订单进度-根据状态查询")
    @ApiOperation(value = "半成品销售订单进度-根据状态查询", notes = "半成品销售订单进度-根据状态查询")
    @GetMapping(value = "/getByStatus")
    public Result<List<SemiProductSalesOrderProgress>> getByStatus(@RequestParam(name = "orderId", required = true) String orderId,
                                                                  @RequestParam(name = "status", required = true) Integer status) {
        try {
            List<SemiProductSalesOrderProgress> progressList = progressService.getByStatus(orderId, status);
            return Result.OK(progressList);
        } catch (Exception e) {
            log.error("根据状态查询进度记录失败", e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 根据操作类型查询进度记录
     */
    @AutoLog(value = "半成品销售订单进度-根据操作类型查询")
    @ApiOperation(value = "半成品销售订单进度-根据操作类型查询", notes = "半成品销售订单进度-根据操作类型查询")
    @GetMapping(value = "/getByOperationType")
    public Result<List<SemiProductSalesOrderProgress>> getByOperationType(@RequestParam(name = "operationType", required = true) String operationType) {
        try {
            List<SemiProductSalesOrderProgress> progressList = progressService.getByOperationType(operationType);
            return Result.OK(progressList);
        } catch (Exception e) {
            log.error("根据操作类型查询进度记录失败", e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 获取订单最新进度
     */
    @AutoLog(value = "半成品销售订单进度-获取最新进度")
    @ApiOperation(value = "半成品销售订单进度-获取最新进度", notes = "半成品销售订单进度-获取最新进度")
    @GetMapping(value = "/getLatestProgress")
    public Result<SemiProductSalesOrderProgress> getLatestProgress(@RequestParam(name = "orderId", required = true) String orderId) {
        try {
            SemiProductSalesOrderProgress progress = progressService.getLatestByOrderId(orderId);
            return Result.OK(progress);
        } catch (Exception e) {
            log.error("获取最新进度失败", e);
            return Result.error("获取失败：" + e.getMessage());
        }
    }

    /**
     * 批量添加进度记录
     */
    @AutoLog(value = "半成品销售订单进度-批量添加")
    @ApiOperation(value = "半成品销售订单进度-批量添加", notes = "半成品销售订单进度-批量添加")
    @PostMapping(value = "/batchAdd")
    public Result<String> batchAdd(@RequestBody List<SemiProductSalesOrderProgress> progressList) {
        try {
            boolean result = progressService.batchInsertProgress(progressList);
            if (result) {
                return Result.OK("批量添加成功!");
            } else {
                return Result.error("批量添加失败!");
            }
        } catch (Exception e) {
            log.error("批量添加进度记录失败", e);
            return Result.error("批量添加失败：" + e.getMessage());
        }
    }

    /**
     * 导出excel
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SemiProductSalesOrderProgress progress) {
        return super.exportXls(request, progress, SemiProductSalesOrderProgress.class, "半成品销售订单进度");
    }

    /**
     * 通过excel导入数据
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SemiProductSalesOrderProgress.class);
    }
}
